package dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import model.PhoneInfo;
import model.PhoneModelInfo;

public class PhoneDao {
	public List getPhoneBrandList() throws SQLException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select phone_brand from phone_info";
        List<PhoneInfo> phoneInfoList = queryRunner.query(sql,new BeanListHandler<PhoneInfo>(PhoneInfo.class));
//        System.out.println(phoneInfoList.get(0).getPhone_brand());
//        String[] phoneBrandArray;
        return phoneInfoList;
	}
	/*
	 * 做个后台管理页面，可以改变phone_price
	 */
	public PhoneModelInfo getPhonePriceAndReviews(String phoneModelName) throws SQLException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		QueryRunner queryRunner = new QueryRunner(dataSource);
		String sql = "select phone_model_reviews,phone_model_price from phone_model_info where phone_model_name ="+"'"+phoneModelName+"'";
		PhoneModelInfo phoneModelInfo = queryRunner.query(sql,new BeanHandler<PhoneModelInfo>(PhoneModelInfo.class));
		return phoneModelInfo;
	}
	public List<PhoneModelInfo> getPhoneNameList() throws SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		QueryRunner queryRunner = new QueryRunner(dataSource);
		String sql = "select phone_model_name from phone_model_info";
		List<PhoneModelInfo> phoneNameList = queryRunner.query(sql,new BeanListHandler<PhoneModelInfo>(PhoneModelInfo.class));
		return phoneNameList;
	}
	public boolean updatePhonePrice(String phoneName,int phonePrice) throws SQLException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		QueryRunner queryRunner = new QueryRunner(dataSource);
		String sql = "update phone_model_info set phone_model_price=? where phone_model_name=?";
		int row = queryRunner.update(sql,phonePrice,phoneName);
		if(row>0) {
			return true;
		}else {
			return false;
		}
	}
}
